const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require("mongoose");
const User = require('../models/Users')
const Admins = require('../models/Admins')
const secretOrKey = require('./env').secretOrkey
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();  //通过配置信息来生成jwt的请求，验证这个token
opts.secretOrKey = secretOrKey
 
module.exports = passport =>{
     passport.use(new JwtStrategy(opts,async function(jwt_payload,done){
      const user = await User.findById(jwt_payload.id)
      const admin = await Admins.findById(jwt_payload.id)
      if(user){
        // 如果有用户信息
        return(done(null, user))
      }
      else if(admin){
        return(done(null, admin))
      }
      else{
        // 没有用户信息
        return(done(null, false))
      }
    })
  )
}
